package com.app.controller;

import com.app.common.Result;
import com.app.model.DutiesModel;
import com.app.service.DutiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

//职务管理
@Controller
@RestController
@RequestMapping("/api/duties")
public class DutiesController {
    @Autowired
    private DutiesService dutiesService;

    //添加和编辑职务
    @PostMapping("/insertAndUpdateDuties")
    public Result<Object> insertAndUpdateDuties(@RequestBody DutiesModel dutiesModel) {
        if (dutiesModel.getId() == 0) {
            dutiesModel.setCreateTime(LocalDateTime.now());
            dutiesService.insert(dutiesModel);
        } else {
            dutiesModel.setUpdateTime(LocalDateTime.now());
            dutiesService.update(dutiesModel);
        }
        return Result.success("操作成功");
    }

    //删除
    @GetMapping("/deleteDuties")
    public Result<Object> deleteDutiesById(int id) {
        dutiesService.deleteDutiesById(id);
        return Result.success("操作成功！");
    }

    //查询职务列表
    @GetMapping("/selectDuties")
    public Result<Object> selectDuties(Integer page, Integer limit, String keyword, Integer type) {
        return Result.success(dutiesService.selectDuties(page, limit, keyword, type));
    }
}
